﻿import com.dinmax.flash.central.extension.*
import com.dinmax.flash.central.extension.user.*
import com.dinmax.flash.central.communication.*
import com.dinmax.flash.central.extension.shortcut.*
import com.dinmax.flash.utilities.xml.XMLUtils

class com.dinmax.flash.central.communication.CommunicationEventFactory {
		
	public static function getComunicationEvent(doc:XML):CommunicationEvent {
		var node:XMLNode = XMLUtils.getNode(doc, "action");
		if( node != undefined){
			switch(XMLUtils.getAttribute(node, "name")){
				case "loginresponse":
					var event:CommunicationEventLoginResponse = new CommunicationEventLoginResponse(CommunicationEvent.getIDCount(), new Date());		
					
					var parameter:XMLNode = XMLUtils.getNode(node, "parameter");
					var extensionID:Number = Number(XMLUtils.getAttribute(parameter, "value"));
					event.setData(extensionID);
					return event;
				break;
				
				case "refreshshortcuts":
					var event2:CommunicationEventRefreshShortcuts = new CommunicationEventRefreshShortcuts(CommunicationEvent.getIDCount(), new Date());
					event2.setData(ShortcutFactory.getShortcuts(node));
					return event2;
				break;
				
				case "getpersonsresponse":
					var event3:CommunicationEventGetPersonsResponse = new CommunicationEventGetPersonsResponse();
					var xmlUsers:Array = XMLUtils.getNodes(node, "user");
					var users:Array = new Array();
					for (var i : Number = 0; i < xmlUsers.length; i++) {
						users.push(UserFactory.getUser(xmlUsers[i]));
					};
					var xmlContacts:Array = XMLUtils.getNodes(node, "contact");
					var contacts:Array = new Array();
					for (var i : Number = 0; i < xmlContacts.length; i++) {
						contacts.push(ContactFactory.getContact(xmlContacts[i]));
					};
					var response:Array = users.concat(contacts);
					event3.setData(response);
					return event3;	
				break;
				
				default:
					throw new Error("CommunicationEventFactory.as | no reconozco la acción\n"+ XMLUtils.getAttribute(node, "name"));
				break;
				
			};
						
		} else {
		
			var event4:CommunicationEventSetExtensions = new CommunicationEventSetExtensions(CommunicationEvent.getIDCount(), new Date());
			event4.setData(ExtensionFactory.getExtensions(doc));
			
			return event4;
		
		};
		
	}; // fin funci	
	
}; // fin de clase
